<?php

function sendContent($data) { //发送邮箱和短信
    $content_change = changeSendContent($data);
    $content_change['mtype'] = $data['code'];
    if ($content_change['code'] == 200) {
        $config_send = json_decode(getTableField("emails", "three"), true);
        if ($config_send['is_start'] == 1) {
            sendEmailMore($content_change, $config_send);
        }
    }
    return $content_change;
}

function changeSendContent($data) { //更改内容
    $code = $data['code'];
    $email_content = M("email_content")->field("title,content,accounts")->where("code = '" . $code . "' AND is_show = 1")->find();
    if ($email_content == '') {
        return array("error" => "请检测邮箱发送内容！", "code" => "content_error");
    }
    if ($code == 'order_success') {
        $rs = getSendOrder($data['order_id'], $email_content);
    }
    return $rs;
}

function getSendOrder($order_id, $contents) {//订单内容
    $order = M("order")->field("addtime,order_money,order_no,uid")->where("id = " . $order_id . "")->find();
    $userinfo = M("user")->field("email,name")->where("id = " . $order['uid'] . "")->find();
    $data['email'] = getImplode($userinfo['email'], $contents['accounts']);
    if ($data['email'] == '') {
        return array("error" => "没有可发送的邮箱", "code" => "email_empty");
    }
    $title = $contents['title'];
    $content = $contents['content'];


    foreach ($userinfo as $k => $v) {
        $order[$k] = $v;
    }
    $order_detail = M("order_details")->where("order_id = " . $order_id . "")->find();
    foreach ($order_detail as $k => $v) {
        $order[$k] = $v;
    }
    $order['goods'] = '';
    if (strstr($content, "{goods}")) {
        $goods = M("order_goods")->field("goods_name,goods_sku_ids,goods_amount,goods_price,goods_id")->where("order_id = " . $order_id . "")->select();
        foreach ($goods as $k => $v) {
            $order['goods'] .= "<span style='margin-right:10px'>" . $v['goods_name'] . "*" . $v['goods_amount'] . "</span>";
        }
    }
    $order['addtime'] = date("Y-m-d H:i");
    foreach ($order as $k => $v) {
        $content = str_replace("{" . $k . "}", $order[$k], $content);
        $title = str_replace("{" . $k . "}", $order[$k], $title);
    }
    $data['title'] = $title;
    $data['content'] = $content;
    $data['code'] = 200;
    return $data;
}

function postSMS($mobiel, $data) {

    $post_data = array();
    $post_data['account'] = "SHHZSW";
    $post_data['pswd'] = "Tch507943";
    $post_data['mobile'] = $mobiel;
    $post_data['msg'] = $data;
    $url = 'http://222.73.117.158/msg/HttpBatchSendSM?';
    $o = "";
    foreach ($post_data as $k => $v) {
        $o.= "$k=" . urlencode($v) . "&";
    }
    $post_data = substr($o, 0, -1);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    $result = curl_exec($ch);
    curl_close($ch);
}

function getLanBalance($result) {
    switch ($result[1]) {
        case 0:
            $rs = "剩余{$result[3]}条";
            break;
        case 101:
            $rs = '无此用户';
            break;
        case 102:
            $rs = '密码错';
            break;
        case 103:
            $rs = '查询过快';
            break;
    }
    return $rs;
}

function sendMobile($code, $phones, $paras, $uid) {
    $phones_remove_code = removeEightSix($phones);
    $content = getMobileContent($code, $paras);
//    echo $content;exit;
    if ($content == '') {
        $result['result'] = "未开启该模板，短信内容为空";
        $result['code'] = 'template_no_start';
    } else {
        import("Common.Org.Chuanglan");

        //国内号码

        $pattern = "/^1[3|4|5|7|8][0-9]{9}$/";
        $content = "【猫翻】" . $content;
        if (preg_match($pattern, $phones_remove_code)) {
            $account = 'N3213317';
            $pwd = 'Psf6e504';
            //国内号码
            $sms = new ChuanglanSMS($account, $pwd);
            $msg_state = $sms->send($phones_remove_code, $content);
            $msg_states = json_decode($msg_state, true);
            $state = $msg_states['success'] === true ? 200 : 0;
        } else {
            $account = 'I5940479';
            $pwd = 'e58f0fe8';
            //国际号码
            $sms = new ChuanglanSMS($account, $pwd, 2);
            $msg_state = $sms->sendInternational($phones_remove_code, $content);
            $msg_states = json_decode($msg_state, true);
            $state = $msg_states['success'] == 1 ? 200 : 0;
        }

        /*         * ****发送短信内容**** */
        $phonesArr = explode(",", $phones);
        if ($phones) {
            $data['phone'] = $phones;
            $data['content'] = $content;
            $data['uid'] = $uid;
            $data['state'] = $state;
            $data['addtime'] = time();
            $data['num'] = count($phonesArr);
            $data['code'] = $code;
            $data['ip'] = getIP();
            M("shorts")->add($data);
            if ($data['state'] == 200) {
                $result['result'] = "ok";
                $result['code'] = 0;
            } else {
                $result['result'] = "发送失败，请联系客服";
                $result['code'] = $msg_states['r'];
            }
        } else {
            $result['result'] = "请输入手机号";
            $result['code'] = "phone_none";
        }
    }
    return $result;
}

function getMobileContent($code, $paras) {
    $rs = "";
    if ($code) {
        $detail = M("shorts_template")->field("content,is_show")->where("code ='" . $code . "'")->find();
        $content = $detail['content'];
        if ($detail['is_show'] == 1) {
//            print_r($paras);
            if ($paras) {
                foreach ($paras as $k => $v) {
                    $content = str_replace("{" . $k . "}", $v, $content);
                }
            }
            $rand = rand(1000, 9999);
            $content = str_replace("{rand}", $rand, $content);
            $time = date("m-d H:i:s");
            $content = str_replace("{time}", $time, $content);
            $rs = $content;
        }
        return $rs;
    }
}

function getSendCode($phone_email, $code) {
    $pattern = "/^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/";
    if (preg_match($pattern, $phone_email)) {
        $detail = M("email_record")->field("content,id")->where("accounts = '" . $phone_email . "'  AND code ='" . $code . "'")->order("id DESC")->find();
    } else {
        $detail = M("shorts")->field("content,id")->where("phone = '" . $phone_email . "'  AND code ='" . $code . "'")->order("id DESC")->find();
//        file_put_contents("aa_test.txt", M("shorts")->getLastSql());
    }
//    echo M("shorts")->getlastsql();
//  print_r($detail_short);
    $rand = preg_replace('/\D/', '', $detail['content']);
    $rs = array("rand" => $rand, "id" => $detail['id']);
    return $rs;
}

function getContentCode($content) {
    return preg_replace('/\D/', '', $content);
}

function checkSendPhoneCode($phone, $code, $code_post, $is_delete) {
    $detail = M("shorts")->field("content,id")->where("phone = '" . $phone . "' AND code ='" . $code . "'")->find();
    $code_save = preg_replace('/\D/', '', $detail['content']);
    $rs = array("code" => 0, "result" => "ok");
    if ($code_save != $code_post) {
        $rs = array("code" => "code_error", "result" => "短信验证码输入错误");
    } else {
        //验证通过，删除验证码
        if ($is_delete == 1) {
            M("shorts")->where("id = " . $detail['id'] . "")->delete();
        }
    }
    return $rs;
}
